package mashibing.class10;

/**
 * 递归遍历二叉树
 *
 * @author yujiafan
 * @version 1.0.0
 * @create 2023-05-16
 */
public class Class10_2_RecursiveTraversalBT {

    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int v) {
            value = v;
        }
    }

    public static void f(Node root) {
        if (root == null) {
            return;
        }
        // 1
        f(root.left);
        // 2
        f(root.right);
        // 3
    }

    // 先序遍历
    public static void pre(Node root) {
        if (root == null) {
            return;
        }
        System.out.println(root.value);
        pre(root.left);
        pre(root.right);
    }

    // 中序遍历
    public static void in(Node root) {
        if (root == null) {
            return;
        }
        pre(root.left);
        System.out.println(root.value);
        pre(root.right);
    }

    // 后序遍历
    public static void pos(Node root) {
        if (root == null) {
            return;
        }
        pre(root.left);
        pre(root.right);
        System.out.println(root.value);
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        head.left = new Node(2);
        head.right = new Node(3);
        head.left.left = new Node(4);
        head.left.right = new Node(5);
        head.right.left = new Node(6);
        head.right.right = new Node(7);

        pre(head);
        System.out.println("========");
        in(head);
        System.out.println("========");
        pos(head);
        System.out.println("========");

    }
}
